var updateFront = "";
function updateFrontTaskList(repeat){
	//var timeRepeat = 5000;
	var timeRepeat = 60000;
	if(!$("#scrollTask").is(":visible")){
		if(repeat) updateFront = window.setTimeout(function(){updateFrontTaskList(repeat);}, timeRepeat);
		return;
	}
	componentLoading($("#scrollTask"));
	simpleserviceWithError("/taskService",{func:"GetFrontTask"},function(data,repeat){
		var objArray = JSON.parse(data);
		$("#tasklist #default_status").html(objArray.Count.N);
		$("#tasklist #warning").html(objArray.Count.W);
		$("#tasklist #critical").html(objArray.Count.C);
		$("#scrollTask table").find("tr").remove();
		for(var i=0;i<objArray.Task.length;i++){
			var rName = objArray.Task[i].r;
			var nameRoomTask = (rName == null || rName =="")? "" : '('+rName+')';
			var isStart = (objArray.Task[i].i=="t")? '<span class="isStarTask">**</span>' : '';
			var style = 'style="margin-left:5px;"';
			if(objArray.Task[i].s == "W") style += 'class="warNingStatus"';
			if(objArray.Task[i].s == "C") style += 'class="criTicalStatus"';
			$("#scrollTask table").append('<tr taskid='+objArray.Task[i].id+'>'+
				'<td colspan="2" style="width:500px; overflow:hidden;">'+
				'<span style="margin-left:10px;"><input  onchange="finishTask(this.name);" type="checkbox" name="'+objArray.Task[i].id+'"   id="'+objArray.Task[i].id+'_Chk">'+		
				'<span '+style+'>'+isStart+objArray.Task[i].t+'</span><span '+style+'>'+nameRoomTask+'</span></td>'+
				'</tr>'+
				'<tr taskbotid='+objArray.Task[i].id+'><td width="100%" align="center" colspan="2" style="padding: 0px; margin: 0px; height: 1px;" >'+
					'<p class="bottom"></p>'+
					'</td>'+
				'</tr>');
		}
		warn.innerHTML = "&nbsp;";
		closeComponentLoading($("#scrollTask"));
		if(repeat) updateFront = window.setTimeout(function(){updateFrontTaskList(repeat);}, timeRepeat);
	},function(data,repeat){
		errorTestTask();
		closeComponentLoading($("#scrollTask"));
		if(repeat) updateFront = window.setTimeout(function(){updateFrontTaskList(repeat);}, timeRepeat);
	},repeat);
}
function errorTestTask(){
	warn.innerHTML ="<img src=\"images/warning.png\" width=\"15px\" height=\"15px\"/>";
}
function getTaskListAll() {
	var url = "/taskService";
	var data = { func : "GetTaskListAll" };
	simpleservice(url,data,function(data){
		var objArray = JSON.parse(data);
		var default_status = 0;
		var warning = 0;
		var critical = 0;
		for(var countTask = 0;countTask<objArray.length;countTask++)
			if(objArray[countTask].status=="Normal")
				default_status++;
		$("#default_status").html(default_status);
	});
}
var TimeOutFunction = [];
function finishTask(id_int){
	clearTimeout(updateFront);
	updateFront = window.setTimeout(function(){updateFrontTaskList(true);}, 10000);
	var close_chk = $("[name="+id_int+"]").attr('checked');
	if($("[name="+id_int+"]").parent().parent().attr("class")==undefined||$("[name="+id_int+"]").parent().parent().attr("class")=="")
		$("[name="+id_int+"]").parent().parent().addClass("lineTask");
	else
		$("[name="+id_int+"]").parent().parent().removeClass("lineTask");
	
	var trAttr =$("#"+id_int+"_Chk").parent().parent().parent();
	var tdAttr = $("#"+id_int+"_Chk").parent().parent();
	if(close_chk=='checked'){
		close_chk=false;
		var link = "/taskService?func=editClose&complete=" + close_chk+"&id_int="+id_int;
		simpleservice(link,null,function(data) {EditTaskStatus(id_int,data);});
		TimeOutFunction[id_int] = window.setTimeout(function(){delayCloseTask(id_int,trAttr,tdAttr);}, 10000);
	}else{
		close_chk=true;
		var link = "/taskService?func=editClose&complete="+ close_chk+"&id_int="+id_int;
		simpleservice(link,null,function(data) {EditTaskStatus(id_int,data);});
		window.clearTimeout(TimeOutFunction[id_int]);
		tdAttr.css('color','#000');
		tdAttr.css('text-decoration','');
	}
}
function delayCloseTask(id_int,trAttr,tdAttr){
	trAttr.find("td:last").remove();
	tdAttr.css('color','#9C9C9C');
	tdAttr.css('text-decoration','line-through');
	trAttr.append(tdAttr);
	$("[taskid="+id_int+"]").remove();
	$("[taskbotid="+id_int+"]").remove();
	getTaskListAll();
}
function popupAddTask(){
	$("#popupAddTask").css("display","");
	$("#popupAddTask").css("z-index","100000");
	$("#popupAddTask").css("position","absolute");
	$("#popupAddTask").css("box-shadow","1px 4px 10px #000");
	$("#popupAddTask").offset({top:380,left:128});
	var top_value = $("#popupAddTaskNote").offset().top;
	var left_value = ($("#popupAddTaskNote").offset().left-400);
	$('#popupAddTask').offset({ top: top_value, left: left_value });
//	$("#popupAddTask").css("background-color","#FFF");
	$("#popupAddTask").draggable();
	$("#value_addtask_popup").select();
	$("#value_addtask_popup").focus();
}
function closePopupAddTask(){
	$("#popupAddTask").css("display","none");
}
function AddTaskNoteFontKey(e) {
	if(e.keyCode == '13') AddTaskNoteFont();
}
function  AddTaskNoteFont() {
	var d1 = new Date();
	var Date_Format = "0000-00-00 00:00:00";
	var value = $('#value_addtask_popup').val();
	var url = "/taskService?func=AddTaskNoteFont&detail=&due="+Date_Format+'&isStar=false&roomName=&status=Normal&taskTitle='+value;
	simpleservice(url,null,function(data){
		document.getElementById('value_addtask_popup').value='';
		$("#popupAddTask").css("display","none");
		updateFrontTaskList(false);
	});
}

///////////
function showTaskListAll(){
	var textAll = "";
	var headHtml = '<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' +
	'<title>Room Reservation Check In</title>' +
	'<script src="js/jquery-1.8.3.min.js"></script>'+
	'<link href="https://695.staygrid.com/styles/billmsg.css" rel="stylesheet" type="text/css">' +
	'<style media="print">' +
	'.dont-print-buttom{' +
	'display:none;' +
	'}' +
	'P.pagebreakhere {page-break-before: always}' +
	'</style>'+
	'</head><body>';
	simpleserviceWithError("/taskService",{func:"GetFrontTaskAll"},function(data,repeat){
		var objArray = JSON.parse(data);
		$("#tasklist #default_status").html(objArray.Count.N);
		$("#tasklist #warning").html(objArray.Count.W);
		$("#tasklist #critical").html(objArray.Count.C);
		for(var i=0;i<objArray.Task.length;i++){
			var rName = objArray.Task[i].r;
			var nameRoomTask = (rName == null || rName =="")? "" : '('+rName+')';
			var isStart = (objArray.Task[i].i=="t")? '<span class="isStarTask">**</span>' : '';
			var style = 'style="margin-left:5px;"';
			if(objArray.Task[i].s == "W") style += 'class="warNingStatus"';
			if(objArray.Task[i].s == "C") style += 'class="criTicalStatus"';
			textAll+='<tr taskid='+objArray.Task[i].id+'>'+
				'<td colspan="2" style="width:500px; overflow:hidden;">'+
				'<span style="margin-left:10px;"><input  onchange="finishTask(this.name);" type="checkbox" name="'+objArray.Task[i].id+'"   id="'+objArray.Task[i].id+'_Chk">'+
				'<span '+style+'>'+isStart+objArray.Task[i].t+'</span><span '+style+'>'+nameRoomTask+'</span></td>'+
				'</tr>'+
				'<tr taskbotid='+objArray.Task[i].id+'><td width="100%" align="center" colspan="2" style="padding: 0px; margin: 0px; height: 1px;" >'+
					'<p class="bottom"></p>'+
					'</td>'+
				'</tr>';
		}
		window.open('','','width=783,height=722').document.write(headHtml+"<table width=\"100%\"><tr><td align=\"center\" colspan=\"3\">TaskList"+
				"<hr></td>"+textAll+"<tr><td><hr></td></tr>"+
				"</table>"+'</body></html>');
		window.open('','','width=783,height=722').focus();
	});
}

function updateLastStay(repeat){
	//service to update
	var url = "/taskService";
	var data = { func : "UpdateLastStay" }
	simpleserviceWithArgs(url,data,updateLastStaySuccess,repeat);
}

function updateLastStaySuccess(data,repeat){
	if(data.trim()=="null"){
		var buttons = [
			{ title:"OK" , callback: function(thisdialog,args){
				window.location.assign("logout.jsp");
			}}
		];
		customDialog(Warning,ForceLogout,null,buttons,null);
	}
	window.setTimeout(function(){updateLastStay(repeat);}, 30000);	
}

function EditTaskStatus(taskId,data){
	var objArray = JSON.parse(data);
	var checkLogOut = CheckSession(objArray);
	if(checkLogOut)
		return;
	for(var i=0;i<TabsData.length;i++){
		TabsData[i].Task = TabsData[i].Task.filter(function(a){return typeof a !== 'undefined';});
		for(var j =0;j<TabsData[i].Task.length;j++){
			if(TabsData[i].Task[j]!=null){
				if(TabsData[i].Task[j].id==taskId){
					for(var k = 0;k<TabsData[i].Task.length;k++){
						if(TabsData[i].Task[j].id==TabsData[i].Task[k].id){
							TabsData[i].Task[j] = objArray;
						}
					}
				}
			}
		}
	}
}